/*
    The MIT License

    Copyright (c) 2010 IFMO/GameDev Studio

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
*/

#include "../core/core.h"
#include "console.h"
#include "../interfaces/int_engine.h"
#include "../core/windows/win_key_list.h"

/*-----------------------------------------------------------------------------
	Core external stuff :
-----------------------------------------------------------------------------*/

//
//	EConsole::EConsole
//
EConsole::EConsole( void )
{
	font		=	UserInterface()->CreateFont("syschar.fnt");
	activated	=	false;
	input_state	=	0;
	opacity		=	0;
}


//
//	EConsole::~EConsole
//
EConsole::~EConsole( void )
{

}


//
//	EConsole::Draw
//
void EConsole::Draw( bool full, uint dtime )
{
	Update();

	if (activated) {	
		opacity	=	clamp<float>( opacity + 5.0*dtime/1000.0f, 0, 1);
	} else {
		opacity	=	clamp<float>( opacity - 5.0*dtime/1000.0f, 0, 1);
	}

	uint w, h, h2;
	rs()->GetScreenSize(w, h);
	uint fh	=	font->GetLineHeight();
	uint fw	=	font->GetCharWidth(' ');
	
	if (full) {
		h2	=	h * 5 / 8;
	} else {
		h2	=	h;
	}
	
	rs()->GetUIScene()->SetColorMultiplier( EColor(1,1,1, opacity) );

	UserInterface()->DrawRect(0, 0, w, h2, "*white", EColor::kBlack * 0.75f );
	
	//
	//	Draw version :
	//
	//if (true) {
	//	uint	len	=	strlen(CoreVersion());	
	//	font->DrawString( w - fw*len, h, CoreVersion(), EColor::kRed );
	//}

	//
	//	Draw last strings :
	//
	if (true) {
		uint num			=	h / fh;
		
		ELogMessage_s	msg;
		for (uint i=0; i<num; i++) {
			Log()->GetLogMessage( i, msg );
			
			EColor	color	=	EColor::kWhite;
			
			if (msg.msg_type==LOG_MSG_WARNING)	color	=	EColor::kRed + EColor::kGreen;
			if (msg.msg_type==LOG_MSG_ERROR)	color	=	EColor::kRed;
			if (msg.msg_type==LOG_MSG_SCRIPT)	color	=	EColor::kWhite * 0.5f;
			
			color	=	color.LerpTo( EColor::kWhite, 0.33f );
			
			font->DrawString( 0, h2 - (i+1) * fh, msg.message, color );
		}
	}	

	//
	//	Draw cmd line :
	//
	if (true) {
		UserInterface()->DrawRect(0, h2-fh, w, fh, "*white", EColor::kWhite * 0.25f );
		font->DrawString( 0*fw, h2, "]", EColor::kWhite );
		font->DrawString( 1*fw, h2, cmdline.Text(), EColor::kWhite );
		
		float	flicker	=	sin( 3 * PI * 0.001 * sys()->Milliseconds() );
		UserInterface()->DrawRect((1+cmdline.Cursor())*fw, h2-fh+fh-2, fw, 2, "*white", EColor::kWhite * flicker );
	}	
}


//
//	EConsole::Update
//
void EConsole::Update( void )
{
	char	ch	=	InputSystem()->GetKbChar();
	char	key	=	InputSystem()->GetKbKey();
	
	if ( key==KEY_BACKSPACE		) {	cmdline.Backspace();	return;	} else
	if ( key==KEY_LEFT			) {	cmdline.Left();			return;	} else
	if ( key==KEY_RIGHT			) {	cmdline.Right();		return;	} else
	if ( key==KEY_DEL			) {	cmdline.Delete();		return;	} else

	if ( key==KEY_ENTER			) {
		LOGF("]%s", cmdline.Text());
		CoreExecuteString( cmdline.Text() );
		helper.AddLastCommand( cmdline.Text() );
		cmdline.Clear();
		return;
	}
	
	if (key==KEY_ESCAPE) {
		Activate(false);
		return;
	}
	
	if (key==KEY_UP) {
		cmdline.SetString( helper.GetPrevCommand() );
		return;
	}
	
	if (key==KEY_DOWN) {
		cmdline.SetString( helper.GetNextCommand() );
		return;
	}
	
	if (ch) {
		cmdline.InsertChar( ch );
	}
}


//
//	EConsole::Activate
//
void EConsole::Activate( bool activate )
{
	if (activate & activated) return;
	if (!activate & !activated) return;
	
	activated	=	activate;

	if (activated) {	
		input_state	=	InputSystem()->GetInputMode();
		InputSystem()->SetInputMode( 0 );
	} else {
		InputSystem()->SetInputMode( input_state );
	}
}

/*-----------------------------------------------------------------------------
	CmdLine :
-----------------------------------------------------------------------------*/

EConsole::CmdLine::CmdLine(void) 
{
	Clear();
}

EConsole::CmdLine::~CmdLine(void) 
{
}

void EConsole::CmdLine::SetString(const char *str)
{
	cmdline = str;
	cursor  = cmdline.length();
}

void EConsole::CmdLine::Clear(void)
{	
	SetString("");
}

void EConsole::CmdLine::InsertChar(char ch)
{
	cmdline.insert( cmdline.begin()+cursor, ch);
	Right();
}

void EConsole::CmdLine::Backspace(void)
{
	if ( cursor>=1 ) {
		cmdline.erase( cmdline.begin()+cursor-1 );
		Left();
	}
}

void EConsole::CmdLine::Delete(void)
{
	if ( cursor < (int)cmdline.length() ) {
		cmdline.erase( cmdline.begin()+cursor );
	}
}

void EConsole::CmdLine::Left(void)
{
	cursor--;
	if (cursor<0) cursor = 0;
	if (cursor>(int)cmdline.length() ) cursor = cmdline.length();
}

void EConsole::CmdLine::Right(void)
{
	cursor++;
	if (cursor<0) cursor = 0;
	if (cursor>(int)cmdline.length()) cursor = cmdline.length();
}


/*-----------------------------------------------------------------------------
	Helper :
-----------------------------------------------------------------------------*/

void EConsole::Helper::AddLastCommand( const char *text )
{
	lastcmds.push_back( text );
	index	=	lastcmds.size();
}


const char* EConsole::Helper::GetPrevCommand( void )
{
	if (lastcmds.empty()) {
		return "";
	}
	index = clamp<int>( index-1, 0, lastcmds.size()-1);
	const char *pstr = lastcmds[ index ].c_str();
	return pstr;
}


const char* EConsole::Helper::GetNextCommand( void )
{
	if (lastcmds.empty()) {
		return "";
	}
	index = clamp<int>( index+1, 0, lastcmds.size()-1);
	const char *pstr = lastcmds[ index ].c_str();
	return pstr;
}

